package com.cqupt.controller;

import com.cqupt.pojo.ResponseMessage;
import com.cqupt.service.FileManagerService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@RequestMapping("fileManager")
@ResponseBody
public class FileManagerController {

    @Autowired
    private FileManagerService fileManagerService;

    /**
     * 附件下载
     * @param body
     * @param request
     * @param response
     */
    @RequestMapping(value = "/download")
    @ResponseBody
    public void download(@RequestBody Map<String, Object> body,
                         HttpServletRequest request,
                         HttpServletResponse response)  {
        // 获取文件地址
        String filedir = (String) body.get("dir");
        //System.out.println("download_filedir："+filedir);
        // 文件下载
        boolean flag = fileManagerService.downloadFile(filedir,request,response);
    }

    /**
     * 文件下载（已弃用）
     * @param body
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/download2",method = RequestMethod.POST,produces = {"application/json;charset = utf-8"})
    public ResponseEntity<byte[]> download2(@RequestBody Map<String, Object> body,
                                            HttpServletRequest request,
                                            HttpServletResponse response) throws IOException {
        // 文件存放地址
        String filedir = (String) body.get("file_dir");
        File file = new File(filedir);
        String name = filedir.substring(filedir.lastIndexOf("_")+1);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.setContentDispositionFormData("attachment", name);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK);
    }


    /**
     * 根据id查看附件
     * @param body
     * @return
     */
    @RequestMapping(value = "/getClosureByID")
    @ResponseBody
    public ResponseMessage getClosureByKnowledgeID(@RequestBody Map<String, Object> body){
        // 获取知识id
        int id = (int) body.get("id");
        List<Map<String,Object>> closures = fileManagerService.getClosuresByID(id);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("closures",closures);
        ResponseMessage responseMessage = new ResponseMessage();
            responseMessage.setStatus(true);
            responseMessage.setContent(resultMap);
        return responseMessage;
    }
}
